﻿<%@ Page Language="C#" AutoEventWireup="true" %>

<% 
    string mappid = "wx718c3d3a69668661";
    if (string.IsNullOrEmpty(Request.QueryString["code"]))
    {
        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE&component_appid={2}#wechat_redirect",
                mappid,
                System.Web.HttpUtility.UrlEncode(Request.Url.AbsoluteUri),
                weixin.api.WXConfig.sAppID
            ));
    }
    else
    {
        string url = string.Format("https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={0}&code={1}&grant_type=authorization_code&component_appid={2}&component_access_token={3}", mappid, Request["code"], weixin.api.WXConfig.sAppID, weixin.api.WXComponent.GetComponentAccessToken(weixin.api.WXConfig.sAppID));
        System.Net.WebClient wc = new System.Net.WebClient();
        string jsonstr = wc.DownloadString(url);
        Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(jsonstr);
        string openid = json.GetValue("openid").ToString();
        Response.Redirect("http://localhost:42926/user/auth?openid=" + openid);
    }  
%>